--Inport: PMEngine, PrivateDB, ProfileDB, GlobalDB
local PMEngine, L, PrivateDB, ProfileDB, GlobalDB = unpack(select(2, ...)); 
local CS = PMEngine:NewModule('ChatSpam')
PMEngine.ChatSpam = CS

local DEFAULT_WIDTH = 890;
local DEFAULT_HEIGHT = 768;
local AC = LibStub("AceConfig-3.0")
local ACD = LibStub("AceConfigDialog-3.0")

CS.Options = {
	type = "group",
	name = 'ChatSpam',
	args = {},
};

CS.Options.args = {
	ChatSpam_Options_header = {
		order = 1,
		type = "header",
		name = "Chat Spam Options",
		width = "full",		
	},	
	ChatSpam_Features_header = {
		order = 2,
		type = "header",
		name = "Chat Spam Features",
		width = "full",	
	},
	ChatSpam_Message_1 = {
		order = 3,
		type = 'group',
		disabled = function() return PMEngine.db.chatspam['enable'] end,
		name = 'First Message',
		guiInline = true,
		args = {
			--description = {
			--	order = 1,				
			--	type = 'description',
			--	fontSize = 'medium',
			--	name = 'First Message'
			--},
			toggle = {
				order = 2,
				type = 'toggle',
				name = 'Enable',
				disabled = function() return PMEngine.db.chatspam['enable'] end,
				get = function(info) return PMEngine.db.chatspam.spamOne['enable'] end,
				set = function(info, value) PMEngine.db.chatspam.spamOne['enable'] = value end
			},
			message = {
				order = 3,
				type = 'input',
				width = 'full',
				multiline = 2,
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamOne['enable'] 
				end,
				name = '',
				get = function(info) return PMEngine.db.chatspam.spamOne['message'] end,
				set = function(info, value) PMEngine.db.chatspam.spamOne['message'] = value end
			},
			interval = {
				order = 4,
				type = 'input',
				name = 'Interval',
				desc = 'Interval in which chat message will be displayed.',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamOne['enable']
				end,
				validate = function(info, value) 
					if tonumber(value) == nil then

						return 'Interval must be a number'
					end
					return true
				end,
				get = function(info) return tostring(PMEngine.db.chatspam.spamOne['interval']) end,
				set = function(info, value) PMEngine.db.chatspam.spamOne['interval'] = tonumber(value) end
			},
			channel = {
				order = 5,
				name = 'Select Channel',
				desc = 'Select a channel to post this messsage in.',
				type = 'select',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamOne['enable'] 
				end,
				values = function()
					local channels = { GetChannelList() }
					local channels_user = {}
					if IsInGuild() then 
						local guildName, guildRank = GetGuildInfo('player')
						channels_user['guild'] = guildName
					end
					channels_user['yell'] = 'Yell'
					channels_user['say'] = 'Say'
						local iter = 0
					for i,title in ipairs(channels) do
						if i % 2 == 0 then
							iter = iter + 1
							channels_user[tostring(iter)] = title
						end
					end
						return channels_user	
				end,
				get = function(info) return PMEngine.db.chatspam.spamOne['channel'] end,
				set = function(info, value) PMEngine.db.chatspam.spamOne['channel'] = value end
			}
		}
	},
	ChatSpam_Message_2 = {
		order = 4,
		type = 'group',
		disabled = function() return PMEngine.db.chatspam['enable'] end,
		name = 'Second Message',
		guiInline = true,
		args = {
			--description = {
			--	order = 1,				
			--	type = 'description',
			--	fontSize = 'medium',
			--	name = 'First Message'
			--},
			toggle = {
				order = 2,
				type = 'toggle',
				name = 'Enable',
				disabled = function() return PMEngine.db.chatspam['enable'] end,
				get = function(info) return PMEngine.db.chatspam.spamTwo['enable'] end,
				set = function(info, value) PMEngine.db.chatspam.spamTwo['enable'] = value end
			},
			message = {
				order = 3,
				type = 'input',
				width = 'full',
				multiline = 2,
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamTwo['enable'] 
				end,
				name = '',
				get = function(info) return PMEngine.db.chatspam.spamTwo['message'] end,
				set = function(info, value) PMEngine.db.chatspam.spamTwo['message'] = value end
			},
			interval = {
				order = 4,
				type = 'input',
				name = 'Interval',
				desc = 'Interval in which chat message will be displayed.',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamTwo['enable']
				end,
				validate = function(info, value) 
					if tonumber(value) == nil then

						return 'Interval must be a number'
					end
					return true
				end,
				get = function(info) return tostring(PMEngine.db.chatspam.spamTwo['interval']) end,
				set = function(info, value) PMEngine.db.chatspam.spamTwo['interval'] = tonumber(value) end
			},
			channel = {
				order = 5,
				name = 'Select Channel',
				desc = 'Select a channel to post this messsage in.',
				type = 'select',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamTwo['enable'] 
				end,
				values = function()
					local channels = { GetChannelList() }
					local channels_user = {}
					if IsInGuild() then 
						local guildName, guildRank = GetGuildInfo('player')
						channels_user['guild'] = guildName
					end
					channels_user['yell'] = 'Yell'
					channels_user['say'] = 'Say'
						local iter = 0
					for i,title in ipairs(channels) do
						if i % 2 == 0 then
							iter = iter + 1
							channels_user[tostring(iter)] = title
						end
					end
						return channels_user	
				end,
				get = function(info) return PMEngine.db.chatspam.spamTwo['channel'] end,
				set = function(info, value) PMEngine.db.chatspam.spamTwo['channel'] = value end
			}
		}
	},
	ChatSpam_Message_3 = {
		order = 5,
		type = 'group',
		disabled = function() return PMEngine.db.chatspam['enable'] end,
		name = 'Third Message',
		guiInline = true,
		args = {
			--description = {
			--	order = 1,				
			--	type = 'description',
			--	fontSize = 'medium',
			--	name = 'First Message'
			--},
			toggle = {
				order = 2,
				type = 'toggle',
				name = 'Enable',
				disabled = function() return PMEngine.db.chatspam['enable'] end,
				get = function(info) return PMEngine.db.chatspam.spamThree['enable'] end,
				set = function(info, value) PMEngine.db.chatspam.spamThree['enable'] = value end
			},
			message = {
				order = 3,
				type = 'input',
				width = 'full',
				multiline = 2,
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamThree['enable'] 
				end,
				name = '',
				get = function(info) return PMEngine.db.chatspam.spamThree['message'] end,
				set = function(info, value) PMEngine.db.chatspam.spamThree['message'] = value end
			},
			interval = {
				order = 4,
				type = 'input',
				name = 'Interval',
				desc = 'Interval in which chat message will be displayed.',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamThree['enable']
				end,
				validate = function(info, value) 
					if tonumber(value) == nil then

						return 'Interval must be a number'
					end
					return true
				end,
				get = function(info) return tostring(PMEngine.db.chatspam.spamThree['interval']) end,
				set = function(info, value) PMEngine.db.chatspam.spamThree['interval'] = tonumber(value) end
			},
			channel = {
				order = 5,
				name = 'Select Channel',
				desc = 'Select a channel to post this messsage in.',
				type = 'select',
				disabled = function() 
					if PMEngine.db.chatspam['enable'] then return true end
					return not PMEngine.db.chatspam.spamThree['enable'] 
				end,
				values = function()
					local channels = { GetChannelList() }
					local channels_user = {}
					if IsInGuild() then 
						local guildName, guildRank = GetGuildInfo('player')
						channels_user['guild'] = guildName
					end
					channels_user['yell'] = 'Yell'
					channels_user['say'] = 'Say'
						local iter = 0
					for i,title in ipairs(channels) do
						if i % 2 == 0 then
							iter = iter + 1
							channels_user[tostring(iter)] = title
						end
					end
						return channels_user	
				end,
				get = function(info) return PMEngine.db.chatspam.spamThree['channel'] end,
				set = function(info, value) PMEngine.db.chatspam.spamThree['channel'] = value end
			}
		}
	},
	ChatSpam_Execution = {
		order = 6,
		type = 'group',
		name = 'Execution',
		disabled = function() return not PMEngine.db.chatspam.spamOne['enable'] end,
		guiInline = true,
		args = {
			--[[interval = {
				order = 1,
				type = 'input',
				name = 'Interval',
				desc = 'Interval in which chat messages will be displayed.',
				disabled = function() 
					return PMEngine.db.chatspam['enable']
				end,
				validate = function(info, value) 
					if tonumber(value) == nil then

						return 'Interval must be a number'
					end
					return true
				end,
				get = function(info) return tostring(PMEngine.db.chatspam['interval']) end,
				set = function(info, value) PMEngine.db.chatspam['interval'] = tonumber(value) end
			},]]
			start = {
				order = 2,
				type = "execute",
				name = "Start",
				desc = "Start Spamming",
				disabled = function()
					if not PMEngine.db.chatspam.spamOne['enable'] 
						and not PMEngine.db.chatspam.spamTwo['enable'] 
						and not PMEngine.db.chatspam.spamThree['enable']
					then 
						return true
					end
					return PMEngine.db.chatspam['enable']
				end,
				confirm = true,
				confirmText = 'Start Spamming?',
				func = function() 
					CS:EnableSpam()
				end,
			},
			stop = {
				order = 3,
				type = "execute",
				name = "Stop",
				desc = "Stop Spamming",
				disabled = function() 
					return not PMEngine.db.chatspam['enable']
				end,
				func = function() 
					CS:DisableSpam()
				end,
			},
		}
	}
}

function CS:ToggleConfig()
	local mode = 'Close'
	if not ACD.OpenFrames['ChatSpam'] then
		mode = 'Open'
	end

	ACD[mode](ACD, 'ChatSpam')

	GameTooltip:Hide()
end

function CS:SendChatMessage()
	local message1 = PMEngine.db.chatspam.spamOne.message
	local channel1 = PMEngine.db.chatspam.spamOne.channel
	local interval1 = PMEngine.db.chatspam.spamOne.interval
	
	local message2 = PMEngine.db.chatspam.spamTwo.message
	local channel2 = PMEngine.db.chatspam.spamTwo.channel
	local interval2 = PMEngine.db.chatspam.spamTwo.interval
	
	local message3 = PMEngine.db.chatspam.spamThree.message
	local channel3 = PMEngine.db.chatspam.spamThree.channel
	local interval3 = PMEngine.db.chatspam.spamThree.interval
	
	--local interval = PMEngine.db.chatspam.interval
	
	
	if PMEngine.db.chatspam.spamOne.enable then
		if time() - CS.lastSpamOne >= interval1 then	
			if channel1 == "guild" then 
				SendChatMessage(message1,"GUILD" ,nil);
			elseif channel1 == "say" then
				SendChatMessage(message1,"SAY" ,nil);
			elseif channel1 == "yell" then
				SendChatMessage(message1,"YELL" ,nil);
			else
				SendChatMessage(message1,"CHANNEL" ,nil, channel1);
			end
			
			CS.lastSpamOne = time();
		end
	end
	
	if PMEngine.db.chatspam.spamTwo.enable then
		if time() - CS.lastSpamTwo >= interval2 then
			if channel2 == "guild" then 
				SendChatMessage(message2,"GUILD" ,nil);
			elseif channel2 == "say" then
				SendChatMessage(message2,"SAY" ,nil);
			elseif channel2 == "yell" then
				SendChatMessage(message2,"YELL" ,nil);
			else
				SendChatMessage(message2,"CHANNEL" ,nil, channel2);
			end
			
			CS.lastSpamTwo = time();
		end
	end
	
	if PMEngine.db.chatspam.spamThree.enable then
		if time() - CS.lastSpamThree >= interval3 then	
			if channel3 == "guild" then 
				SendChatMessage(message3,"GUILD" ,nil);
			elseif channel3 == "say" then
				SendChatMessage(message3,"SAY" ,nil);
			elseif channel3 == "yell" then
				SendChatMessage(message3,"YELL" ,nil);
			else
				SendChatMessage(message3,"CHANNEL" ,nil, channel3);
			end

			CS.lastSpamThree = time();
		end
	end
end

function CS:EnableSpam()
	PMEngine.db.chatspam['enable'] = true
	self.Frame:SetScript('OnUpdate', self.SendChatMessage)
end
function CS:DisableSpam()
	PMEngine.db.chatspam['enable'] = false
	--CS.LastSpammed = 0
	CS.lastSpamOne = 0
	CS.lastSpamTwo = 0
	CS.lastSpamThree = 0
	self.Frame:SetScript('OnUpdate', nil)
end

function CS:Initialize()
	AC:RegisterOptionsTable('ChatSpam', CS.Options)
	ACD:SetDefaultSize('ChatSpam', DEFAULT_WIDTH, DEFAULT_HEIGHT)
	self.lastSpamOne = 0
	self.lastSpamTwo = 0
	self.lastSpamThree = 0
	self.Frame = CreateFrame('Frame')	
	if PMEngine.db.chatspam.enable then
		self.Frame:SetScript('OnUpdate', self.SendChatMessage)
	end

	--self:ToggleSpam()
end

PMEngine:RegisterModule(CS:GetName())